// When color definition differs for dark and light variant,
// it gets @if ed depending on $variant

@import 'color-palette';

@function gtkalpha($color, $alpha) {
  @return unquote("alpha(#{$color}, #{$alpha})");
}

// Determine whether the color has alpha.
@function has-alpha($color) {
  @return if(alpha($color) < 1, true, false);
}

// Determine whether the color is "light" or "dark".
@function tone($color) {
  // Calculate color brightness as per https://www.w3.org/TR/AERT/#color-contrast
  $brightness: (red($color) * 299 + green($color) * 587 + blue($color) * 114) / 1000;

  @if $brightness >= 128 {
    @return "light";
  } @else {
    @return "dark";
  }
}

$on-light: #000;
$on-dark: #fff;

// Determine whether to use dark or light color on top of given color
// to meet accessibility standards for contrast.
@function on($color) {
  $contrast-color: if(tone($color) == "dark", $on-dark, $on-light);

  @if saturation($color) > 50% or alpha($color) < 1 {
    @return $contrast-color;
  } @else {
    @return rgba($contrast-color, .87);
  }
}

// Determine the strength of highlight on top of given color.
@function highlight($color) {
  @if lightness($color) >= 80% {
    @return rgba(white, .4);
  } @else if lightness($color) >= 40% {
    @return rgba(white, .2);
  } @else {
    @return rgba(white, .1);
  }
}

// Make translucent color opaque by blending with the background color.
@function opacify($color, $bg) {
  @return mix(change-color($color, $alpha: 1), $bg, alpha($color) * 100%);
}

// Set the accessible opacity for the overlay depending on the given color.
@function overlay($state, $color, $background: transparent, $opacity-modifier: 0) {
  @if saturation($color) > 50% or saturation($background) > 50% {
    $opacity-modifier: .04;
  }

  $opacity: ();

  @if $state == "hover" {
    $opacity: .08 + $opacity-modifier;
  } @else if $state == "hover-alt" {
    $opacity: .04 + $opacity-modifier;
  } @else if $state == "focus" {
    $opacity: .08 + $opacity-modifier;
  } @else if $state == "pressed" {
    $opacity: .12 + $opacity-modifier;
  } @else if $state == "activated" {
    $opacity: .12 + $opacity-modifier;
  } @else if $state == "selected" {
    @return rgba($color, .24);
  } @else {
    @error "Invalid type: '#{$state}'";
  }

  @return mix(rgba($color, 1), $background, $opacity * 100%);
}

// Private variables for dark background colors
$-dark-background: #282A36;
$-dark-surface-1dp: mix(white, $-dark-background, 5%);
$-dark-surface-4dp: mix(white, $-dark-background, 9%);
$-dark-surface-8dp: mix(white, $-dark-background, 12%);
$-dark-surface-switch: mix(white, $-dark-background, 60%);

//
// Main colors
//

$background: #f2f2f2;
$base: #fff; // for views
$base-alt: #fafafa;
$surface: #fff;
$switch-surface: #fff; // Special case for switches
$on-surface: on($surface);

$primary-on-light: #1a73e8;
$primary-on-dark: #BD93F9;
$primary: $primary-on-light;
$on-primary: on($primary);

$error-on-light: #d93025;
$error-on-dark: #E64747;
$error: $error-on-light;
$on-error: on($error);

$warning-on-light: #f4b400;
$warning-on-dark: #EFA554;
$warning: $warning-on-light;
$on-warning: on($warning);

$success-on-light: #0f9d58;
$success-on-dark: #42E66C;
$success: $success-on-light;
$on-success: on($success);

$visited-on-light: $purple-500;
$visited-on-dark: $purple-200;
$visited: $visited-on-light;
$on-visited: on($visited);

$os-background: $-dark-background;
$on-os-background: on($os-background);

$tooltip: rgba(#616161, .9);
$on-tooltip: on($tooltip);

$scrim: rgba(black, .6);
$on-scrim: on($scrim);

$scrim-alt: rgba(black, .3);
$on-scrim-alt: on($scrim-alt);

$panel: darken($-dark-background, 2%);
$on-panel: on($panel);

// for Unity panel which doesn't allow translucent colors
$panel-solid: $panel;
$on-panel-solid: on($panel-solid);

$titlebar: #383838;
$titlebar-backdrop: #303030;
$on-titlebar: on($titlebar);

$titlebar-indicator: currentcolor;

@if $variant == "dark" {
  $background: $-dark-background;
  $base: $-dark-surface-1dp;
  $base-alt: $-dark-surface-1dp;
  $surface: $-dark-surface-8dp;
  $switch-surface: $-dark-surface-switch;
  $on-surface: on($surface);

  $primary: $primary-on-dark;
  $on-primary: on($primary);

  $error: $error-on-dark;
  $on-error: on($error);

  $warning: $warning-on-dark;
  $on-warning: on($warning);

  $success: $success-on-dark;
  $on-success: on($success);

  $visited: $visited-on-dark;
  $on-visited: on($visited);

  $titlebar: $-dark-surface-4dp;
  $titlebar-backdrop: $-dark-surface-1dp;
  $on-titlebar: on($titlebar);
}

@if $topbar == "light" {
  $panel: $scrim;
  $on-panel: on($panel);

  $panel-solid: #ccc;
  $on-panel-solid: on($panel-solid);

  $titlebar: #e0e0e0;
  $titlebar-backdrop: #d6d6d6;
  $on-titlebar: on($titlebar);

  $titlebar-indicator: $primary;
}

//
// Overlay state colors
//

$overlay-selected: rgba($primary, .24);

//
// For “on” colors
//

@function primary($color) {
  @return if(tone($color) == "dark", $primary-on-light, $primary-on-dark);
}

@function error($color) {
  @return if(tone($color) == "dark", $error-on-light, $error-on-dark);
}

@function hint($color) {
  @return rgba($color, if(has-alpha($color), .6, .7));
}

@function disabled($color) {
  @return rgba($color, if(has-alpha($color), .38, .5));
}

@function disabled-hint($color) {
  @return rgba($color, if(has-alpha($color), .26, .3));
}

@function stroke($color) {
  @return rgba($color, if(has-alpha($color), .26, .3));
}

@function disabled-stroke($color) {
  @return rgba($color, if(has-alpha($color), .12, .2));
}

@function divider($color) {
  @return rgba($color, if(has-alpha($color), .12, .2));
}

@function fill($color) {
  @return rgba($color, if(has-alpha($color), .08, .08));
}

@function entry-fill($color) {
  @return rgba($color, if(has-alpha($color), .04, .04));
}
